package addressBook;

import java.awt.event.*;
import java.io.*;

import javax.swing.SwingUtilities;
import javax.swing.event.*;

public class simpleWindowController implements ActionListener, ListSelectionListener, MouseListener, KeyListener{
	
	private simpleWindowView theView;
	private simpleWindowModel theModel;
	private PopUpMenu menu;
	
	public simpleWindowController(final simpleWindowView theView, simpleWindowModel theModel, PopUpMenu theMenu)
	{
		this.theView = theView;
		this.theModel = theModel;
		this.menu = theMenu;
		
		theModel.addObserver(theView);
		
		this.theView.addNewContactListener(this);
		this.theView.loadListener(this);
		this.theView.saveContactsListener(this);
		this.theView.exitListener(this);

		this.theView.searchContactListener(this);
		this.theView.removeContactListener(this);
		this.theView.sortByFirstListener(this);
		this.theView.sortByLastListener(this);
		this.theView.sortByPhoneListener(this);
		this.theView.sortByAddressListener(this);
		this.theView.sortByCityListener(this);
		this.theView.sortByUrlListener(this);
		this.theView.sortByEmailListener(this);
		
		this.theView.updateListener(this);
		TableCellListener e = null;
		this.theView.tableModificationListener(e);
		this.theView.listListener(this);
		this.theView.selectedRow(this);
		this.theView.selectedKeyboardRow(this);
		this.theView.selectedListKeyBoardRow(this);
		
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		
		if(arg0.getSource() == theView.aFileItem){
			
			theModel.openNewContactWindow(theView.myContacts);
			
		}
		else if(arg0.getSource() == theView.svFileItem){
			try{
				
				saveTable();
				
			}
			catch(Exception ex){
				System.out.println(ex.getMessage());
			}

		}
		else if(arg0.getSource() == theView.lFileItem){
			try{
				
				loadTable();
				
			}
			catch(Exception ex){
				System.out.println(ex.getMessage());
			}

		}
		else if(arg0.getSource() == theView.eFileItem) System.exit(0);
		else if(arg0.getSource() == theView.rEditItem) theModel.removeContactWindow(theView.myContacts);
		else if(arg0.getSource() == theView.sEditItem) theModel.openSearchWindow(theView.myContacts);
		else if(arg0.getSource() == theView.fSortItem){

			theView.myContacts.setArg(0);
			theView.myContacts.sortContacts(theView.myContacts.getArg());
			theView.fSortItem.setState(true);
			theView.lSortItem.setState(false);
			theView.pSortItem.setState(false);
			theView.aSortItem.setState(false);
			theView.cSortItem.setState(false);
			theView.uSortItem.setState(false);
			theView.eSortItem.setState(false);
			
		}
		else if(arg0.getSource() == theView.lSortItem){

			theView.myContacts.setArg(1);
			theView.myContacts.sortContacts(theView.myContacts.getArg());
			theView.fSortItem.setState(false);
			theView.lSortItem.setState(true);
			theView.pSortItem.setState(false);
			theView.aSortItem.setState(false);
			theView.cSortItem.setState(false);
			theView.uSortItem.setState(false);
			theView.eSortItem.setState(false);
			
		}
		else if(arg0.getSource() == theView.pSortItem){

			theView.myContacts.setArg(2);
			theView.myContacts.sortContacts(theView.myContacts.getArg());
			theView.fSortItem.setState(false);
			theView.lSortItem.setState(false);
			theView.pSortItem.setState(true);
			theView.aSortItem.setState(false);
			theView.cSortItem.setState(false);
			theView.uSortItem.setState(false);
			theView.eSortItem.setState(false);
			
		}
		else if(arg0.getSource() == theView.aSortItem){

			theView.myContacts.setArg(3);
			theView.myContacts.sortContacts(theView.myContacts.getArg());
			theView.fSortItem.setState(false);
			theView.lSortItem.setState(false);
			theView.pSortItem.setState(false);
			theView.aSortItem.setState(true);
			theView.cSortItem.setState(false);
			theView.uSortItem.setState(false);
			theView.eSortItem.setState(false);
			
		}
		else if(arg0.getSource() == theView.cSortItem){

			theView.myContacts.setArg(4);
			theView.myContacts.sortContacts(theView.myContacts.getArg());
			theView.fSortItem.setState(false);
			theView.lSortItem.setState(false);
			theView.pSortItem.setState(false);
			theView.aSortItem.setState(false);
			theView.cSortItem.setState(true);
			theView.uSortItem.setState(false);
			theView.eSortItem.setState(false);
			
		}
		else if(arg0.getSource() == theView.uSortItem){

			theView.myContacts.setArg(5);
			theView.myContacts.sortContacts(theView.myContacts.getArg());
			theView.fSortItem.setState(false);
			theView.lSortItem.setState(false);
			theView.pSortItem.setState(false);
			theView.aSortItem.setState(false);
			theView.cSortItem.setState(false);
			theView.uSortItem.setState(true);
			theView.eSortItem.setState(false);
			
		}
		else if(arg0.getSource() == theView.eSortItem){

			theView.myContacts.setArg(6);
			theView.myContacts.sortContacts(theView.myContacts.getArg());
			theView.fSortItem.setState(false);
			theView.lSortItem.setState(false);
			theView.pSortItem.setState(false);
			theView.aSortItem.setState(false);
			theView.cSortItem.setState(false);
			theView.uSortItem.setState(false);
			theView.eSortItem.setState(true);
			
		}
		else if(arg0.getSource() == theView.button){
			if(theView.getList().getSelectedIndex()!=-1){
				theView.setSelectedContact(new contact(theView.myContacts.getContacts()[theView.getList().getSelectedIndex()]));
				theView.myContacts.getContacts()[theView.myContacts.idContact(theView.getSelectedContact())][0] = theView.textField1.getText();
				theView.myContacts.getContacts()[theView.myContacts.idContact(theView.getSelectedContact())][1] = theView.textField2.getText();
				theView.myContacts.getContacts()[theView.myContacts.idContact(theView.getSelectedContact())][2] = theView.textField3.getText();
				theView.myContacts.getContacts()[theView.myContacts.idContact(theView.getSelectedContact())][3] = theView.textField4.getText();
				theView.myContacts.getContacts()[theView.myContacts.idContact(theView.getSelectedContact())][4] = theView.textField5.getText();
				theView.myContacts.getContacts()[theView.myContacts.idContact(theView.getSelectedContact())][5] = theView.textField6.getText();
				theView.myContacts.getContacts()[theView.myContacts.idContact(theView.getSelectedContact())][6] = theView.textField7.getText();
	
				String fN = theView.textField1.getText();
				String lN = theView.textField2.getText();
				fN = fN.concat(" ").concat(lN);
				Object fnLn = fN;
				
				theView.myContacts.modifyContact(fnLn, theView.getList().getSelectedIndex());
		        theView.myContacts.sortContacts(theView.myContacts.getArg());
			}
			else{
				theView.textField1.setText("");
				theView.textField2.setText("");
				theView.textField3.setText("");
				theView.textField4.setText("");
				theView.textField5.setText("");
				theView.textField6.setText("");
				theView.textField7.setText("");
			}
		}
		else if(arg0.getSource() == menu.rItem){
			if(theView.getTable().getSelectedRow()<theView.myContacts.numContacts()) theView.myContacts.removeContact(new contact(theView.myContacts.getContacts()[theView.getTable().getSelectedRow()]));
		}
		else if(arg0.getSource() == null) System.out.println("null button");
		theModel.changed();
	}
	
	public void saveTable() throws Exception
	{
		
		PrintWriter prw = new PrintWriter(new FileWriter("Data.csv"));
		for (int i = 0 ; i < theView.getTable().getRowCount(); i++)
		{
		    for(int j = 0 ; j < theView.getTable().getColumnCount();j++)
		    {
		    	prw.print((String)(theView.getTable().getValueAt(i,j)));
		    	if(j!=theView.getTable().getColumnCount())
		    		prw.print(":");
		    }
		    prw.print("\n");
		}
			prw.close();
			
	}
	
	public void loadTable() throws Exception
	{
		BufferedReader bfr = new BufferedReader(new FileReader("Data.csv"));
		String data = null;
        while ((data=bfr.readLine())!=null){
        	String[] array1 = data.split(":");
        	if(!array1[0].equals("null"))
        		theView.myContacts.addContact(new contact(array1));
        }
        theView.myContacts.sortContacts(theView.myContacts.getArg());
        theModel.changed();
		bfr.close();
	}

	@Override
	public void valueChanged(ListSelectionEvent arg0) {
		if (!arg0.getValueIsAdjusting()) {
    		String selectedContact = null;
    		if(theView.getList().getSelectedValue() != null)
    			selectedContact = new String(theView.getList().getSelectedValue().toString());
    		String[] fnLn = null;
    		String[] finall = new String[7];
    		if (selectedContact!=null){
    			fnLn = selectedContact.split("\\s+");
    		}
    		if(fnLn!=null){
        		finall[0] = fnLn[0];
        		finall[1] = fnLn[1];
        		finall[2] = "";
        		finall[3] = "";
        		finall[4] = "";
        		finall[5] = "";
        		finall[6] = "";
        		theView.setSelectedContact(new contact(finall));
        		theView.textField1.setText(theView.myContacts.getContacts()[theView.myContacts.idContact(theView.getSelectedContact())][0]);
        		theView.textField2.setText(theView.myContacts.getContacts()[theView.myContacts.idContact(theView.getSelectedContact())][1]);
        		theView.textField3.setText(theView.myContacts.getContacts()[theView.myContacts.idContact(theView.getSelectedContact())][2]);
        		theView.textField4.setText(theView.myContacts.getContacts()[theView.myContacts.idContact(theView.getSelectedContact())][3]);
        		theView.textField5.setText(theView.myContacts.getContacts()[theView.myContacts.idContact(theView.getSelectedContact())][4]);
        		theView.textField6.setText(theView.myContacts.getContacts()[theView.myContacts.idContact(theView.getSelectedContact())][5]);
        		theView.textField7.setText(theView.myContacts.getContacts()[theView.myContacts.idContact(theView.getSelectedContact())][6]);
        		theView.button.setEnabled(true);
            	theView.getTable().setRowSelectionInterval(theView.myContacts.idContact(theView.getSelectedContact()), theView.myContacts.idContact(theView.getSelectedContact()));
    		}
        }
	}

	@Override
	public void mouseClicked(MouseEvent arg0) {
		theView.getList().setSelectedIndex(theView.getTable().getSelectedRow());
		theModel.changed();
		if(SwingUtilities.isRightMouseButton(arg0)){
			menu = new PopUpMenu();
			menu.show(arg0.getComponent(), arg0.getX(), arg0.getY());
			menu.removeContactListener(this);
		}
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		
	}

	@Override
	public void keyPressed(KeyEvent arg0) {
		if(arg0.getKeyChar() == '\u0008'){
			theView.myContacts.removeContact(new contact(theView.myContacts.getContacts()[theView.getTable().getSelectedRow()]));
			theModel.changed();
		}
	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		theView.getList().setSelectedIndex(theView.getTable().getSelectedRow());
		theModel.changed();
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		
	}
	
}
